package com.diko.configuration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.stereotype.Component;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author Emma Li
 * @date 2020/10/25 - 9:59
 */

public class myApplicationEvent {


    @Bean(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME)
    ApplicationEventMulticaster customApplicationEventMulticaster(ThreadPoolExecutor taskExecutor){
        SimpleApplicationEventMulticaster eventMulticaster = new SimpleApplicationEventMulticaster();
        eventMulticaster.setTaskExecutor(taskExecutor);
        return eventMulticaster;
    }

    @SuppressWarnings("unused")
    @Component
    static class NoticeListener implements ApplicationListener<Lab2AutoConfig.NoticeEvent>{
        private static final Logger logger= LoggerFactory.getLogger(NoticeListener.class);

        @Override
        public void onApplicationEvent(Lab2AutoConfig.NoticeEvent noticeEvent) {
            logger.info("时间监听器获取到 NoticeEvent,睡眠当前线程2秒..");
            try {
                Thread.sleep(2000);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            logger.info("NoticeEvent 的 message 属性是:{}",noticeEvent.getMessage());
        }
    }

}
